<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Widgets;
use Encore\Admin\Layout\Content;
use App\Admin\Forms\Userinfo;

class UserinfoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '个人信息';

    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */
    protected $description = [
        'edit' => '请在此处编辑您的个人信息',
    ];

    /**
     * Edit interface.
     *
     * @param Content $content
     *
     * @return Content|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function detail(Content $content)
    {
        if (\Request::isMethod('put')) {
            return $this->save();
        }

        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body(Widgets\Tab::forms([
                'basic'    => Userinfo\Basic::class,
                'optional' => Userinfo\Optional::class,
                'custom'   => Userinfo\Custom::class,
            ]));
    }

    /**
     * save
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    private function save()
    {
        $this->store();

        admin_toastr('保存成功');
        return redirect('/admin/userinfo');
    }
}
